/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package myjme.appstate;

import Globals.AllGlobal;
import brain.MyBrain;
import com.jme3.app.Application;
import com.jme3.app.SimpleApplication;
import com.jme3.app.state.AbstractAppState;
import com.jme3.app.state.AppStateManager;
import com.jme3.asset.AssetManager;
import com.jme3.input.InputManager;
import com.jme3.input.KeyInput;
import com.jme3.input.controls.ActionListener;
import com.jme3.input.controls.KeyTrigger;
import com.jme3.material.Material;
import com.jme3.math.ColorRGBA;
import com.jme3.math.FastMath;
import com.jme3.math.Ray;
import com.jme3.math.Vector3f;
import com.jme3.renderer.Camera;
import com.jme3.scene.Geometry;
import com.jme3.scene.Node;
import com.jme3.scene.shape.Box;
import com.jme3.scene.shape.Cylinder;
import com.jme3.scene.shape.Line;
import com.jme3.scene.shape.Sphere;
import controller.MyController;
import database.operate.OperateMyDatabaseImpl;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.logging.Level;
import java.util.logging.Logger;
import model.ZuanChang_Model;
import model.ZuanKong_Model;

/**
 *
 * @author Administrator
 */
public class HiddenAppstate extends AbstractAppState{
    private static int ROT_NAME=80000;
    private SimpleApplication app;
    private Camera cam;
    private Node rootNode;
    private Node nodeSmallThanRoot=new Node(java.util.ResourceBundle.getBundle("myproj/globalizefile").getString("NSTRCUBOID"));
    private AssetManager assetManager;
    private InputManager inputManager;
    private Ray ray = new Ray();
    private static Box mesh = new Box(Vector3f.ZERO, 1, 1, 1);
    private MyBrain brain;
    private MyController controller;
    Material mat_good=null;
    Material mat_bad=null;
    Material mat_chuanjiang=null;
    Material mat_small=null;
    Material mat_middl=null;
    Material mat_big=null;
    Sphere ball=new Sphere(16, 16, 1f);
    Material mat_chushuidian=null;
    @Override
    public void initialize(AppStateManager stateManager,Application app) {
        super.initialize(stateManager, app);
        this.app          = (SimpleApplication) app;
        this.cam          = this.app.getCamera();
        this.rootNode     = this.app.getRootNode();
        this.assetManager = this.app.getAssetManager();
        this.inputManager = this.app.getInputManager();
        mat_small=new Material(assetManager, java.util.ResourceBundle.getBundle("myproj/globalizefile").getString("COMMON/MATDEFS/MISC/UNSHADED.J3MD"));
        mat_middl=new Material(assetManager, java.util.ResourceBundle.getBundle("myproj/globalizefile").getString("COMMON/MATDEFS/MISC/UNSHADED.J3MD"));
        mat_big=new Material(assetManager, java.util.ResourceBundle.getBundle("myproj/globalizefile").getString("COMMON/MATDEFS/MISC/UNSHADED.J3MD"));
        mat_good=new Material(assetManager, java.util.ResourceBundle.getBundle("myproj/globalizefile").getString("COMMON/MATDEFS/MISC/UNSHADED.J3MD"));
        mat_bad=new Material(assetManager, java.util.ResourceBundle.getBundle("myproj/globalizefile").getString("COMMON/MATDEFS/MISC/UNSHADED.J3MD"));
        mat_chuanjiang=new Material(assetManager, java.util.ResourceBundle.getBundle("myproj/globalizefile").getString("COMMON/MATDEFS/MISC/UNSHADED.J3MD"));
        mat_chushuidian=new Material(assetManager,java.util.ResourceBundle.getBundle("myproj/globalizefile").getString("COMMON/MATDEFS/MISC/UNSHADED.J3MD"));
        mat_good.setColor(java.util.ResourceBundle.getBundle("myproj/globalizefile").getString("COLOR"), new ColorRGBA(92.0f/255.0f, 167.0f/255.0f, 186.0f/255.0f, 0.75f));
        mat_bad.setColor(java.util.ResourceBundle.getBundle("myproj/globalizefile").getString("COLOR"), new ColorRGBA(255.0f/255.0f, 66.0f/255.0f, 93.0f/255.0f, 0.75f));
        mat_chuanjiang.setColor(java.util.ResourceBundle.getBundle("myproj/globalizefile").getString("COLOR"), ColorRGBA.Blue);
        mat_chushuidian.setColor(java.util.ResourceBundle.getBundle("myproj/globalizefile").getString("COLOR"), new ColorRGBA(230f/255.0f, 254.0f/255.0f ,255/255, 0.75f));
        mat_small.setColor(java.util.ResourceBundle.getBundle("myproj/globalizefile").getString("COLOR"), ColorRGBA.Green);
        mat_middl.setColor(java.util.ResourceBundle.getBundle("myproj/globalizefile").getString("COLOR"), ColorRGBA.Blue);
        mat_big.setColor(java.util.ResourceBundle.getBundle("myproj/globalizefile").getString("COLOR"), ColorRGBA.Red);
        inputManager.addMapping("FindDimensionDrill_Cuboid",new KeyTrigger(KeyInput.KEY_Y));
        /**Use ActionListener to respond to pressed/released inputs (key presses, mouse clicks) */ 
        ActionListener actionListener = new ActionListener(){
               public void onAction(String name, boolean pressed, float tpf){
                   if(name.equals("FindDimensionDrill_Cuboid")&&pressed){
                      for(int i=ROT_NAME-1;i>=80000;i--){
                          boolean flag=false;
                          Geometry boGeo=(Geometry)nodeSmallThanRoot.getChild("box"+i);
                          for(int j=ROT_NAME-1;j>=80000;j--){
                              Geometry compGeo=(Geometry)nodeSmallThanRoot.getChild("box"+j);
                              if(i!=j&&boGeo!=null&&compGeo!=null){
                                  float distance=boGeo.getWorldTranslation().distance(compGeo.getWorldTranslation());
                                  if(boGeo!=null&&compGeo!=null&&distance<25.0f){
                                      flag=true;
                                      break;
                                  }else
                                      flag=false;
                                  
                              }
                          }
                          if(!flag&&boGeo!=null){
                              boGeo.setMaterial(mat_chuanjiang);
                              System.out.println("zheliyouyige");
                          }else
                              System.out.println("meiyoufaxian");
                      } 
                   }
               }
        };

        /** Add an action to one or more listeners */
        inputManager.addListener(actionListener, "FindDimensionDrill_Cuboid");
    }

    @Override
    public void update(float tpf) {
        super.update(tpf); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public void cleanup() {
        super.cleanup();
        while(nodeSmallThanRoot.getChildren().size()>0){
            nodeSmallThanRoot.getChildren().remove(nodeSmallThanRoot.getChildren().size()-1);
        }
    }
    
    public void drawZKAll()throws Exception{ 
        ArrayList<ZuanKong_Model> list=OperateMyDatabaseImpl.getAllResult(AllGlobal.ZUANKONG_SQL);
        Iterator<ZuanKong_Model> it = list.iterator(); 
        while(nodeSmallThanRoot.getChildren().size()>0){
            nodeSmallThanRoot.getChildren().remove(nodeSmallThanRoot.getChildren().size()-1);
        }
        while(it.hasNext()) {  
            Node rotNode=new Node("rot"+(ROT_NAME));
            
            ///////box///////
            Box b=new Box(0.6f, 0.6f, 0.6f);
            Geometry boGeo=new Geometry("box"+(ROT_NAME), b);
            
            boGeo.setMaterial(assetManager.loadMaterial("Materials/ShangFuYanCeng.j3m"));
            ///////box///////
                       
            Geometry geom_1=new Geometry("csd"+(ROT_NAME), ball);          
            geom_1.setMaterial(mat_chushuidian);
            
            rotNode.attachChild(geom_1);
            
            ZuanKong_Model zuanKong_Model = it.next();

//            Line line=new Line(Vector3f.ZERO, new Vector3f(0, 0, -Float.parseFloat(zuanKong_Model.getKongshen())));
            Line line=new Line(Vector3f.ZERO, new Vector3f(0, 0, -250));
            Geometry lineGeo=new Geometry("line"+(ROT_NAME++), line);
            float ck=Float.parseFloat(zuanKong_Model.getChengkong());
            if(ck<=20.0f){
                lineGeo.setMaterial(mat_small);
            }else if(ck>20.0f&&ck<=40.0f){
                lineGeo.setMaterial(mat_middl);
            }else{
                lineGeo.setMaterial(mat_big);
            }
            //这句话是有目的的移动
            ArrayList<ZuanChang_Model> alist=OperateMyDatabaseImpl.getOne(AllGlobal.ZUANCHANG_SQL, 
                        "select * from zuanchang where mingcheng='"+zuanKong_Model.getZuanchang()+"';");
            float rF=brain.CaculateR(Float.parseFloat(zuanKong_Model.getKongshen())-20, Float.parseFloat(alist.get(0).getKongxilvString()), 1, 
                        Float.parseFloat(zuanKong_Model.getVa())+Float.parseFloat(zuanKong_Model.getVb())+Float.parseFloat(zuanKong_Model.getVc())
                        , AllGlobal.CUBOID_TYPE);   
            Box drill=new Box(rF/2, rF/2, Float.parseFloat(zuanKong_Model.getKongshen())/2-10);
            Geometry cg=new Geometry(zuanKong_Model.getMingcheng(), drill);    
            if(Float.parseFloat(zuanKong_Model.getZhongya())<controller.getZhongyajixian()){
                cg.setMaterial(mat_bad);
            }else{
                cg.setMaterial(mat_good);
            }   
            if(Float.parseFloat(zuanKong_Model.getQingjiao())!=0)
                boGeo.move(0, 0, -60/FastMath.sin(Float.parseFloat(zuanKong_Model.getQingjiao())* FastMath.DEG_TO_RAD));
            else
                boGeo.move(0, 0, -60);
            geom_1.move(0, 0, -Float.parseFloat(zuanKong_Model.getChushuidianshuipingtouying())/FastMath.sin(Float.parseFloat(zuanKong_Model.getQingjiao())* FastMath.DEG_TO_RAD));
            cg.move(0, 0, -Float.parseFloat(zuanKong_Model.getKongshen())/2-20+10);
            rotNode.attachChild(cg);
            rotNode.attachChild(boGeo);
            rotNode.attachChild(lineGeo);
            //设定旋转
            rotNode.rotate(-Float.parseFloat(zuanKong_Model.getQingjiao())* FastMath.DEG_TO_RAD,
                        (controller.getFANGWEIJIAO()-Float.parseFloat(zuanKong_Model.getFangwei()))*FastMath.DEG_TO_RAD, 
                        0); 
            //向边缘移动
                
            rotNode.move(0, -2, 2);
            //追随钻场设定位置
            
            if(!alist.isEmpty()){
                ZuanChang_Model zuanChang_Model=alist.get(0);
                if(controller.getGZM_TYPE()==AllGlobal.GZM_TYPE_LEFT){

                    rotNode.move(Float.parseFloat(zuanChang_Model.getXdzbx()),
                            0, 
                            -Float.parseFloat(zuanChang_Model.getXdzby()));
                }else if(controller.getGZM_TYPE()==AllGlobal.GZM_TYPE_RIGHT){

                    rotNode.move(Float.parseFloat(zuanChang_Model.getXdzbx()),
                            0, 
                            -controller.getGzmzxcd()+Float.parseFloat(zuanChang_Model.getXdzby()));
                }
                nodeSmallThanRoot.attachChild(rotNode);   
            }                    
            rootNode.attachChild(nodeSmallThanRoot);
        }
        
        //这里直接进行判断，并且装载入一个
        
    }
    //直接调用数据库进行绘图
    public void readData(){
        
    }
    public void addBrain(MyBrain brain){
        this.brain=brain;
    }
    public void addController(MyController controller){
        this.controller=controller;
    }
    public boolean findGood(int index){//不小于80000的数
        boolean b=true;
        return b;
    }
}

